package com.luoyanni;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        int result = sum(1,2,3,4,5,6,7,8,9,10);
        System.out.println(result);


        extracted();

        test2();
    }
//Ctrl+alt+m 将方法抽取
    private static void extracted() {
        //集合只能存储引用数据类型
        TreeSet<Integer> set1 = new TreeSet<>();
        //1--》包装成Integer对象---》自动包装
        set1.add(5);
        set1.add(1);
        set1.add(3);
        set1.add(4);
        set1.add(2);

        for (Integer  integer : set1){
            System.out.println(integer);
        }
    }

    public static void test1(){
        TreeSet<Student> set = new TreeSet<>();

        set.add(new Student("罗欢", 19));
        set.add(new Student("罗扬", 20));
        set.add(new Student("罗燕妮", 21));
        set.add(new Student("郑乔", 22));

        for (Student student : set){
            System.out.println(student.getName() + "---" + student.getAge());
        }
    }

    public static void test2() {
        /*Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
            //如果是正序排，则计o1-o2
            //如果是倒序排，则计o2-o1
            return o2.getAge() - o1.getAge();
            }
        }*/
        Comparator<Student> comparator = (o1, o2)-> o1.getAge() - o2.getAge();
        TreeSet<Student> set = new TreeSet<>(comparator);
        set.add(new Student("罗欢", 19));
        set.add(new Student("罗扬", 20));
        set.add(new Student("罗燕妮", 21));
        set.add(new Student("郑乔", 22));

        for (Student student : set){
            System.out.println(student.getName() + "---" + student.getAge());
            }
        }

        //可变参数必须是形参最后一个参数
        public static int sum(int...a){
        int total = 0;
        for (int i = 0; i < a.length; i++){
            total += a[i];
            }
        return total;
        }

    }
